
#include <BHBoxLayout>
#include <BTableItem>
#include <BListItem>
#include <BLineEdit>
#include <BClass>

#include "StringUtils.h"
#include "ScriptFormat.h"
#include "InvokeBoard.h"

InvokeBoard::InvokeBoard(BWidget* parent) : BWidget(parent)
{
	_scrollToLastRow = false;

	_tableWidget = new BTableWidget();
	_tableWidget->setColumnCount(2);
	_tableWidget->setColumnLabel(0, "type");
	_tableWidget->setColumnLabel(1, "name");
	_tableWidget->setColumnWidth(0, 200);
	_tableWidget->setColumnWidth(1, 200);
	//_tableWidget->setColumnPolicy(Policy_Fixed | Policy_Expanding);
	_tableWidget->setGridStyle(Grid_Crossing);
	BLayout* layout = new BLayout(this);
	layout->setWidget(_tableWidget);

	connect(this, Signal_AssetChanged, &InvokeBoard::slotAssetChanged);
}
InvokeBoard::~InvokeBoard()
{

}

void InvokeBoard::freshStack()
{
	_tableWidget->clearItems();
	for (int i = 0; i < _script->stackSize(); i++)
	{
		BString type;
		BString name;
		const BValue& value = _script->stack(i, type, name);
		if (type == "call")
		{
			int row = _tableWidget->addRows(1);
			_tableWidget->setRowAsset(row, value);
			_tableWidget->setItem(row, 0, type);
			_tableWidget->setItem(row, 1, name);
			if (BTableItem* item = _tableWidget->item(row, 1))
			{
				ScriptFormat* format = new ScriptFormat();
				format->setAsset(_script);
				item->setTextFormat(format);
			}
		}
	}
	_scrollToLastRow = true;
}

void InvokeBoard::slotAssetChanged(BObject* object, const BValue& value)
{
	BScript* script = value;
	if (_script != script)
	{
		_script = script;
	}
}
void InvokeBoard::slotStackFreshed(BObject* object)
{
	if (_scrollToLastRow)
	{
		int lastRow = _tableWidget->rowCount() - 1;
		BTableItem* item = _tableWidget->item(lastRow, 0);
		_tableWidget->scrollToItem(item);
		_scrollToLastRow = false;
	}
}
